/**
 * 
 */
package org.swing.utility.input.chooser;

import java.io.File;
import java.util.Date;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * @author lqnhu
 *
 */
public class FileChooserUtil {
	public static String getFilePathToSave(String title, String format)
			throws Exception {
		return getFilePathToSave(title, null, format);
	}

	public static String getFolderPath(String title) throws Exception {
		JFileChooser jFileChooser;
		FileNameExtensionFilter filter;
		String rutaGuardar = null;
		jFileChooser = new JFileChooser();
		jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int indicador = 0;
		try {
			jFileChooser.setDialogTitle(title);
			indicador = jFileChooser.showOpenDialog(null);
			if (indicador == JFileChooser.APPROVE_OPTION) {
				rutaGuardar = jFileChooser.getSelectedFile().getAbsolutePath();
			} else {
				throw new Exception("The event was not yes nor ok.");
			}
		} catch (Exception e) {
			throw new Exception(
					"Error when try to get path of file to save it.", e);
		}
		return rutaGuardar;
	}

	public static String getFilePathToSave(String title, String nameToSave,
			String format) throws Exception {
		JFileChooser jFileChooser;
		FileNameExtensionFilter filter;
		String rutaGuardar = null;
		jFileChooser = new JFileChooser();
		filter = new FileNameExtensionFilter("Files " + format, format, format);
		jFileChooser.setFileFilter(filter);
		jFileChooser
				.setSelectedFile(new File(((nameToSave != null && !nameToSave
						.equals("")) ? nameToSave : "file")
						+ "_"
						+ (new Date()).getTime() + "." + format));
		int indicador = 0;
		try {
			jFileChooser.setDialogTitle(title);
			indicador = jFileChooser.showSaveDialog(null);
			if (indicador == JFileChooser.APPROVE_OPTION) {
				rutaGuardar = jFileChooser.getSelectedFile().getAbsolutePath();
			} else {
				throw new Exception("The event was not yes nor ok.");
			}
		} catch (Exception e) {
			throw new Exception(
					"Error when try to get path of file to save it.", e);
		}
		return rutaGuardar;
	}

	public static String getFilePathToOpen(String title, String format)
			throws Exception {
		return getFilePathToOpen(title, null, format);
	}

	public static String getFilePathToOpen(String title) throws Exception {
		return getFilePathToOpen(title, null, null);
	}

	public static String getFilePathToOpen(String title, String defaultFolder,
			String format) throws Exception {
		JFileChooser jFileChooser;
		FileNameExtensionFilter filter;
		String rutaAbrir = null;
		if (defaultFolder != null && !defaultFolder.equals("")) {
			jFileChooser = new JFileChooser(defaultFolder);
		} else {
			jFileChooser = new JFileChooser();
		}
		if (format != null && !format.equals("")) {
			filter = new FileNameExtensionFilter("Files " + format, format,
					format);
			jFileChooser.setFileFilter(filter);
		}
		int indicador = 0;
		try {
			jFileChooser.setDialogTitle(title);
			indicador = jFileChooser.showOpenDialog(null);
			if (indicador == JFileChooser.APPROVE_OPTION) {
				rutaAbrir = jFileChooser.getSelectedFile().getAbsolutePath();
			} else {
				throw new Exception("The event was not yes nor ok.");
			}
		} catch (Exception e) {
			throw new Exception(
					"Error when try to get path of file to open it.", e);
		}
		return rutaAbrir;
	}
}
